package com.kk.base.controller.home;

import com.alibaba.fastjson.JSONObject;
import com.kk.base.bean.CodeMsg;
import com.kk.base.bean.PageBean;
import com.kk.base.bean.Result;
import com.kk.base.entity.common.ExpressMailOrderStatus;
import com.kk.base.entity.home.ExpressMailOrder;
import com.kk.base.service.admin.BuyOffService;
import com.kk.base.service.admin.ProcessRecordsService;
import com.kk.base.service.home.ExpressMailOrderService;
import com.kk.base.util.StringUtil;
import com.kk.base.util.ValidateEntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/home/expressMailOrder")
public class HomeExpressMailOrderController {

    @Autowired
    private ExpressMailOrderService expressMailOrderService;

    @Autowired
    private BuyOffService buyOffService;

    @Autowired
    private ProcessRecordsService processRecordsService;



    @RequestMapping("/orderDetails")
    public String orderManagement(Model model,@RequestParam(name="expressMailOrderId",required=true) Long expressMailOrderId){
        model.addAttribute("processRecordsList",processRecordsService.findByExpressMailOrder_Id(expressMailOrderId));
        model.addAttribute("expressMailOrder",expressMailOrderService.find(expressMailOrderId));
        model.addAttribute("buyOffList",buyOffService.findByBuyOffExpressMailOrder_IdOrderByCreateTime(expressMailOrderId));
        model.addAttribute("index",10);
        return "home/order_management/order_details";
    }


    /**
     * 进行表单验证进行保存
     *
     * @param expressMailOrder
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> add(ExpressMailOrder expressMailOrder) {
        expressMailOrder.setWaybillNumber(StringUtil.GenerateAUniqueNumber());
        //进行实体类验证
        CodeMsg validate = ValidateEntityUtil.validate(expressMailOrder);
        if (validate.getCode() != CodeMsg.SUCCESS.getCode()) {
            return Result.error(validate);
        }

        //验证成功进行保存
        if (expressMailOrderService.save(expressMailOrder) == null) {
            return Result.error(CodeMsg.ADMIN_EXPRESSMAILORDER_ADD_ERROR);
        }
        //保存成功返回成功消息
        return Result.success(true);
    }

    /**
     * 进行设置上门送件时间
     *
     * @param expressMailOrderId
     * @return
     */
    @RequestMapping(value = "/setPickUpTime", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> setPickUpTime(String pickUpTime, Long expressMailOrderId) {
        ExpressMailOrder expressMailOrder = expressMailOrderService.find(expressMailOrderId);
        if (expressMailOrder.getOrderStatus().equals(ExpressMailOrderStatus.SIGNFOR)){
            return Result.error(CodeMsg.ADMIN_EXPRESSMAILORDER_SETPICKUPTIME_ERROR1);
        }
        SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition parseposition = new ParsePosition(0);
        Date date = simpledateformat.parse(pickUpTime, parseposition);
        expressMailOrder.setPickUpTime(date);

        //验证成功进行保存
        if (expressMailOrderService.save(expressMailOrder) == null) {
            return Result.error(CodeMsg.ADMIN_EXPRESSMAILORDER_SETPICKUPTIME_ERROR);
        }
        //保存成功返回成功消息
        return Result.success(true);
    }


    /**
     * 进行表单验证进行保存
     *
     * @param recipientsInfo
     * @return
     */
    @RequestMapping(value = "/saveAdd", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> saveAdd(String recipientsInfo) {
        List<ExpressMailOrder> expressMailOrders = JSONObject.parseArray(recipientsInfo, ExpressMailOrder.class);
        for (ExpressMailOrder expressMailOrder : expressMailOrders) {
            //进行实体类验证
            CodeMsg validate = ValidateEntityUtil.validate(expressMailOrder);
            if (validate.getCode() != CodeMsg.SUCCESS.getCode()) {
                return Result.error(validate);
            }
        }

        //验证成功进行保存
        if (expressMailOrderService.saveAll(expressMailOrders).equals(1)) {
            return Result.error(CodeMsg.ADMIN_EXPRESSMAILORDER_ADD_ERROR);
        }
        //保存成功返回成功消息
        return Result.success(true);
    }


    /**
     * 进行表单验证进行保存
     *
     * @param rsolutionId
     * @return
     */
    @RequestMapping(value = "/selOrderState", method = RequestMethod.POST)
    public String selOrderState(String rsolutionId, String waybillNumberStr,
                                PageBean<ExpressMailOrder> pageBean, Model model) {
        // rsolutionId  2已下单 3处理中  4已完成
        pageBean.setPageSize(5);
        model.addAttribute("pageBean", expressMailOrderService.selOrderState(pageBean, rsolutionId, waybillNumberStr));
        model.addAttribute("rsolutionId", rsolutionId);
        return "home/index/order_state_page";
    }

    /**
     * 删除快递订单
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> delete(@RequestParam(name = "id", required = true) Long id) {
        try {
            expressMailOrderService.deleteById(id);
        } catch (Exception e) {
            return Result.error(CodeMsg.ADMIN_EXPRESSMAILORDER_DELETE_ERROR);
        }
        return Result.success(true);
    }

}
